package com.example.cloud.consume.order.exception;


import com.example.cloud.entities.constant.HttpStatus;
import com.example.cloud.entities.exception.BaseException;
import com.example.cloud.entities.exception.CustomException;
import com.example.cloud.entities.exception.DemoModeException;
import com.example.cloud.entities.utils.SimpleMessage;
import com.example.cloud.entities.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.security.auth.login.AccountExpiredException;
import java.nio.file.AccessDeniedException;
import java.util.List;

/**
 * 全局异常处理器
 *
 * @author zhaodao
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
	private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

	/**
	 * 基础异常
	 */
	@ExceptionHandler(BaseException.class)
	public SimpleMessage baseException(BaseException e) {
		return SimpleMessage.fail(e.getMessage());
	}

	/**
	 * 业务异常
	 */
	@ExceptionHandler(CustomException.class)
	public SimpleMessage businessException(CustomException e) {
		if (StringUtils.isNull(e.getCode())) {
			return SimpleMessage.fail(e.getMessage());
		}
		return SimpleMessage.fail(e.getCode(), e.getMessage());
	}

	@ExceptionHandler(NoHandlerFoundException.class)
	public SimpleMessage handlerNoFoundException(Exception e) {
		log.error(e.getMessage(), e);
		return SimpleMessage.fail(HttpStatus.NOT_FOUND, "路径不存在，请检查路径是否正确");
	}

	@ExceptionHandler(AccessDeniedException.class)
	public SimpleMessage handleAuthorizationException(AccessDeniedException e) {
		log.error(e.getMessage());
		return SimpleMessage.fail(HttpStatus.FORBIDDEN, "没有权限，请联系管理员授权");
	}

	@ExceptionHandler(AccountExpiredException.class)
	public SimpleMessage handleAccountExpiredException(AccountExpiredException e) {
		log.error(e.getMessage(), e);
		return SimpleMessage.fail(e.getMessage());
	}

	@ExceptionHandler(UsernameNotFoundException.class)
	public SimpleMessage handleUsernameNotFoundException(UsernameNotFoundException e) {
		log.error(e.getMessage(), e);
		return SimpleMessage.fail(e.getMessage());
	}


	@ExceptionHandler(value = DataIntegrityViolationException.class)
	@ResponseBody
	public SimpleMessage requestExceptionHandler(DataIntegrityViolationException e) {
		log.error(e.toString());
		return SimpleMessage.fail(HttpStatus.ERROR, "数据操作格式异常");
	}

	@ExceptionHandler(MethodArgumentNotValidException.class)
	@ResponseBody
	public SimpleMessage methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
		log.error(e.toString());
		BindingResult result = e.getBindingResult();
		String s = "参数验证失败";
		if (result.hasErrors()) {
			List<ObjectError> errors = result.getAllErrors();
			s = errors.get(0).getDefaultMessage();
		}
		return SimpleMessage.fail(HttpStatus.ERROR, s);
	}

	@ExceptionHandler(IllegalArgumentException.class)
	@ResponseBody
	public SimpleMessage illegalArgumentException(IllegalArgumentException e) {
		log.error(e.toString());
		return SimpleMessage.fail(HttpStatus.ERROR, "数据异常");
	}

	@ExceptionHandler(Exception.class)
	public SimpleMessage handleException(Exception e) {
		log.error(e.getMessage(), e);
		return SimpleMessage.fail(StringUtils.isNotEmpty(e.getMessage()) ? e.getMessage() : e.toString());
	}

	/**
	 * 演示模式异常
	 */
	@ExceptionHandler(DemoModeException.class)
	public SimpleMessage demoModeException(DemoModeException e) {
		return SimpleMessage.fail("演示模式，不允许操作");
	}
}
